library(ggplot2)
library(data.table)
library(dplyr)Lecture 3: Exercise Solutions
Load packages
1 Exercise 1
Modify the code here to show the relationship between health and wealth for 2002 instead of 2007.
gapminder_filtered <- gapminder |>
filter(year == 2007)
ggplot(data = gapminder_filtered,
mapping = aes(x = gdpPercap, y = lifeExp,
size = pop, color = continent)) +
geom_point() +
scale_x_log10(labels = scales::dollar_format(accuracy = 1)) +
scale_size_continuous(labels = scales::label_comma()) +
scale_color_viridis_d(option = "plasma") +
labs(x = "GDP per capita", y = "Life expectancy",
title = "Health and wealth are strongly related",
subtitle = "142 countries; 2007 only", caption = "Source: The Gapminder Project",
color = "Continent", size = "Population") +
theme_bw()
Hint: You’ll want to change something in the code that creates gapminder_filtered. The text in the subtitle won’t change automatically, so you’ll want to edit that too.